package com.ncloud.resman.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.body.Result;
import com.ncloud.common.utils.ResultUtil;
import com.ncloud.resman.busi.NetWorkExcelBusi;
import com.ncloud.resman.entity.IpAddress;
import com.ncloud.resman.service.impl.IpAddressServiceImpl;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created by gaowei on 2017/8/29.
 */
@RestController
@RequestMapping("/resman/ipaddress")
public class IpAddressController {
	@Autowired
	private IpAddressServiceImpl ipAddressService;
	@Autowired
	private NetWorkExcelBusi netWorkExcelBusi;

    @GetMapping(value = "/list")
    public Result<?> getCabinetInfo(HttpServletRequest request,
    		@RequestParam("netType") String netType,@RequestParam("ipSegment") String ipSegment,
    		@RequestParam("state") String state,@RequestParam("ip") String ip){
    	JSONObject params = new JSONObject();
    	params.put("netType", netType);
    	params.put("ipSegment", ipSegment);
    	params.put("state", state);
    	params.put("ip", ip);
    	JSONArray infos = ipAddressService.getIpAddressInfo(params);
		return ResultUtil.success(infos);
    }   
    @GetMapping(value = "/ipsegmentlist/{netType}")
    public Result<?> getIpSegment(HttpServletRequest request,@PathVariable("netType") String netType){
    	JSONArray infos = ipAddressService.getIpSegment(netType);
		return ResultUtil.success(infos);
    }
    @PostMapping(value = "/addsingle")
    public Result<?> addIPSingle(HttpServletRequest request, @RequestBody IpAddress ipAddress){
    	Object userID = request.getAttribute("userID");
    	ipAddress.setCreateUser(userID==null?1:Long.parseLong(userID.toString()));
    	ipAddress.setCreateDate(new Date());
    	int num = ipAddressService.addIPSingle(ipAddress);
		return ResultUtil.success(num);
    }
    @PostMapping(value = "/addregion")
    public Result<?> addIPRegion(HttpServletRequest request, @RequestBody JSONObject param){
    	Object userID = request.getAttribute("userID");
    	int num = ipAddressService.addIPRegion(userID,param);
		return ResultUtil.success(num);
    }
    @PutMapping(value = "/update")
    public Result<?> update(HttpServletRequest request, @RequestBody IpAddress ipAddress){
    	Object userID = request.getAttribute("userID");
    	ipAddress.setUpdateUser(userID==null?1:Long.parseLong(userID.toString()));
    	ipAddress.setUpdateDate(new Date());
    	int num = ipAddressService.updateIpAddress(ipAddress);
		return ResultUtil.success(num);
    }
    @PutMapping(value = "/allotip")
    public Result<?> allotIpAddress(HttpServletRequest request, @RequestBody IpAddress ipAddress){
    	Object userID = request.getAttribute("userID");
    	ipAddress.setState("1");
    	ipAddress.setUpdateUser(userID==null?1:Long.parseLong(userID.toString()));
    	ipAddress.setUpdateDate(new Date());
    	int num = ipAddressService.allotIpAddress(ipAddress);
		return ResultUtil.success(num);
    }
    @PutMapping(value = "/releaseip")
    public Result<?> releaseIpAddress(HttpServletRequest request, @RequestBody IpAddress ipAddress){
    	Object userID = request.getAttribute("userID");
    	ipAddress.setState("0");
    	ipAddress.setApplyPerson("");
    	ipAddress.setPurpose("");
    	ipAddress.setUpdateUser(userID==null?1:Long.parseLong(userID.toString()));
    	ipAddress.setUpdateDate(new Date());
    	int num = ipAddressService.releaseIpAddress(ipAddress);
		return ResultUtil.success(num);
    }
    @DeleteMapping(value = "/delete/{id}")
    public Result<?> delete(HttpServletRequest request, @PathVariable("id") Long id){
    	int num = ipAddressService.deleteIpAddress(id);
		return ResultUtil.success(num);
    }
    @GetMapping(value = "/isipvalid")
    public Result<?> isIpValid(HttpServletRequest request,@RequestParam("ip") String ip){
    	int tag = ipAddressService.isIpValid(ip);
		return ResultUtil.success(tag);
    }
    @GetMapping(value = "/importIpAddress")
    public Result<?> importIpAddress(HttpServletRequest request,
    		@RequestParam("netType") String netType,@RequestParam("ipSegment") String ipSegment,
    		@RequestParam("state") String state,@RequestParam("ip") String ip,HttpServletResponse response) throws IOException{
    	JSONObject params = new JSONObject();
    	params.put("netType", netType);
    	params.put("ipSegment", ipSegment);
    	params.put("state", state);
    	params.put("ip", ip);
    	netWorkExcelBusi.exportIpAdress(params, response);
		return ResultUtil.success();
    }
    @GetMapping(value = "/importIpAddressTemplate")
    public Result<?> importIpAddressTemplate(HttpServletRequest request,
    		@RequestParam("netType") String netType,@RequestParam("ipSegment") String ipSegment,
    		@RequestParam("state") String state,@RequestParam("ip") String ip,HttpServletResponse response) throws IOException{
    	JSONObject params = new JSONObject();
    	params.put("netType", netType);
    	params.put("ipSegment", ipSegment);
    	params.put("state", state);
    	params.put("ip", ip);
    	netWorkExcelBusi.exportIpAdressTemplate(params, response);
		return ResultUtil.success();
    }
  //导入交换机接口Excel
    @PostMapping(value ="/importIpAddress")
    public Result<?> importIpAddress(HttpServletRequest request, @RequestParam(value="file",required=false) MultipartFile file) throws Exception{
    	Long userID = Long.parseLong(request.getAttribute("userID").toString());
    	JSONObject re=netWorkExcelBusi.importIpAddressInfo(file.getInputStream(),userID);
    	return ResultUtil.success(re);
    }    
}
